/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.webfile.server.impl;

import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.knowledge.doc.domain.Doc;
import com.wallellen.knowledge.doc.domain.ex.DocEntire;
import com.wallellen.knowledge.doc.exception.CanNoWriteException;
import com.wallellen.knowledge.doc.server.DocManagerInter;
import com.wallellen.knowledge.doc.server.DocOperateRightInter;
import com.wallellen.knowledge.doc.server.DocTypeInter;
import com.wallellen.knowledge.doc.server.FileManagerInter;
import com.wallellen.knowledge.webfile.server.WebFileManagerInter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class WebFileManagerImpl implements WebFileManagerInter {
    @Resource
    private DocManagerInter farmDocManagerImpl;
    @Resource
    private FileManagerInter farmFileManagerImpl;
    @Resource
    private DocOperateRightInter farmDocOperateRightImpl;
    @Resource
    private DocTypeInter farmDocTypeManagerImpl;

    @Override
    @Transactional
    public DocEntire creatWebFile(List<String> fileid, String typeId, String fileName, String tag, String groupId,
                                  String text, DocOperateRightInter.POP_TYPE editPop, DocOperateRightInter.POP_TYPE readPop, LoginUser currentUser) {
        Doc doc = new Doc();
        doc.setTitle(fileName);
        doc.setTagkey(tag);
        doc.setDomtype("5");
        doc.setDocgroupid(groupId);
        doc.setReadpop(readPop.getValue());
        doc.setWritepop(editPop.getValue());
        doc.setState("1");
        DocEntire doce = new DocEntire(doc);
        doce.setTexts(text, currentUser);
        doce.setType(farmDocTypeManagerImpl.getType(typeId));
        doce = farmDocManagerImpl.createDoc(doce, currentUser);
        for (String id : fileid) {
            farmFileManagerImpl.addFileForDoc(doce.getDoc().getId(), id, currentUser);
            farmFileManagerImpl.submitFile(id);
        }

        return doce;
    }

    @Override
    public DocEntire editWebFile(String docid, List<String> fileid, String typeId, String fileName, String tag,
                                 String groupId, String text, DocOperateRightInter.POP_TYPE editPop, DocOperateRightInter.POP_TYPE readPop, String editNote, LoginUser currentUser) {
        DocEntire doc = farmDocManagerImpl.getDoc(docid);
        doc.getDoc().setTitle(fileName);
        doc.getDoc().setTagkey(tag);
        doc.getDoc().setDomtype("5");
        doc.getDoc().setDocgroupid(groupId);
        doc.getDoc().setReadpop(readPop.getValue());
        doc.getDoc().setWritepop(editPop.getValue());
        doc.setTexts(text, currentUser);
        doc.setFiles(new ArrayList<>());// 重置附件_zhanghc_20150919
        for (String id : fileid) {
            doc.addFile(farmFileManagerImpl.getFile(id));
        }
        doc.setType(farmDocTypeManagerImpl.getType(typeId));
        try {
            farmFileManagerImpl.delFileForDoc(doc.getDoc().getId(), currentUser);
            doc = farmDocManagerImpl.editDocByUser(doc, editNote, currentUser);
        } catch (CanNoWriteException e) {
            throw new RuntimeException(e);
        }
        return doc;
    }

}
